package com.demo.forum.service2;


import com.demo.forum.model2.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;



/**
 * 定期存款业务
 */
@Service
@Slf4j
public class FixedDeposit implements IDeposit{

    public Calendar calendar  = Calendar.getInstance();

    @Override
    @Transactional
    public void saveAmount(Double money,  Integer month,User user) {
        if(money <= 0){
            log.warn("存款不合法!");
            return;
        }

        if(user.getDeleteState() ==1){
            log.warn("用户已经被冻结");
            return;
        }

        user.setBalance(user.getBalance()+ money);
        calendar.add(Calendar.MONTH, month);
        user.setExpirationTime(calendar.getTime());
        //计算利息
        interest(month,user);
        show(money,month,user);

    }



    @Override
    public void interest(Integer month,User user){
        //设置利息
        if(month <= 0){
           log.warn("参数校验失败!");
        }

        if(month > 0 && month <= 3){
            user.setInterest(user.getBalance() *1.35/100 * month);
        }else if(month > 3 && month <=6){
            user.setInterest(user.getBalance() *1.55/100 * month);
        }else if(month >6 && month <= 12){
            user.setInterest(user.getBalance() *1.75/100 * month);

        }else if(month >12 && month <= 24 ){
            user.setInterest(user.getBalance() *2.25/100 * month);

        }else {
            user.setInterest(user.getBalance() *2.75/100 * month);
        }


    }

    @Override
    public void show(Double money,Integer month,User user) {
        calendar.add(Calendar.MONTH, month);
        user.setExpirationTime(calendar.getTime());
        System.out.println("存款人姓名: "+user.getUsername() +
                ",账号 : "+user.getId() + ",存款金额 " + money +",到期时间: "+user.getExpirationTime()
                +",到期利息 : " + user.getInterest());
    }
}
